<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="50"/>
            <el-table-column prop="user.nickname" label="用户" width="100"/>
            <el-table-column prop="images" label="图片" width="150">
              <template slot-scope="scope">
                <div v-if="scope.row.images">
                  <el-image
                      v-for="(image, index) in getImageList(scope.row.images)"
                      :key="index"
                      :preview-src-list="getImageList(scope.row.images)"
                      style="width: 60px; height: 60px; margin-right: 5px"
                      :src="image" fit="cover"></el-image>
                </div>
                <span v-else>无图片</span>
              </template>
            </el-table-column>
            <el-table-column prop="content" label="内容" min-width="200"/>
            <el-table-column prop="like_count" label="点赞数" width="80"/>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                  {{ scope.row.status === 1 ? '上架' : '下架' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="创建时间" width="160">
              <template slot-scope="scope">
                {{ formatDate(scope.row.create_time) }}
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="120">
              <template slot-scope="scope">
                <el-button 
                  v-if="scope.row.status === 0" 
                  type="text" 
                  style="color: #67C23A" 
                  @click="onStatusChange(scope.row, 1)">
                  上架
                </el-button>
                <el-button 
                  v-if="scope.row.status === 1" 
                  type="text" 
                  style="color: #E6A23C" 
                  @click="onStatusChange(scope.row, 0)">
                  下架
                </el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";

export default {
  name: "UserPostIndex",
  components: {
  },
  mixins: [crud],
  data() {
    return {
      request: http.userPost,
      searchList: [
        {
          label: '内容',
          prop: 'content',
          name: 'input',
          type: 'textarea',
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          options: [
            { label: '上架', value: 1 },
            { label: '下架', value: 0 }
          ]
        }
      ],
      searchParams: {
        contentLike: undefined,
        status: undefined,
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  methods: {
    // 处理图片列表
    getImageList(images) {
      if (!images) return [];
      return images.split(',').filter(img => img.trim());
    },
    
    // 格式化日期
    formatDate(date) {
      if (!date) return '';
      return new Date(date).toLocaleString('zh-CN');
    },
    
    // 状态变更
    async onStatusChange(row, status) {
      const action = status === 1 ? '上架' : '下架';
      try {
        await this.$confirm(`确定要${action}这条动态吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        
        await this.request.update(row.id, { status });
        this.$message.success(`${action}成功`);
        this.initData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error(`${action}失败`);
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
